using Godot;

public partial class Menu : Node2D
{
	private Button _resolutionButton;
	
	public override void _Ready()
	{
		// 获取分辨率按钮的引用
		_resolutionButton = GetNode<Button>("UI/VBoxContainer/ResolutionButton");
		
		// 连接按钮点击事件
		_resolutionButton.Pressed += OnResolutionButtonPressed;
	}
	
	private void OnResolutionButtonPressed()
	{
		// 获取当前显示器的分辨率
		Vector2I screenSize = DisplayServer.ScreenGetSize();
		
		GD.Print($"检测到屏幕分辨率: {screenSize.X} x {screenSize.Y}");
		
		var window = GetWindow();
		
		// 只有在当前不是窗口化模式时才切换到窗口化模式
		if (window.Mode != Window.ModeEnum.Windowed)
		{
			window.Mode = Window.ModeEnum.Windowed;
		}
		
		// 设置窗口大小
		window.Size = screenSize;
		
		// 将窗口居中显示
		Vector2I screenCenter = (DisplayServer.ScreenGetSize() - screenSize) / 2;
		window.Position = screenCenter;
		
		// 更新项目设置中的窗口大小（这会影响项目的默认分辨率）
		ProjectSettings.SetSetting("display/window/size/viewport_width", screenSize.X);
		ProjectSettings.SetSetting("display/window/size/viewport_height", screenSize.Y);
		ProjectSettings.SetSetting("display/window/size/window_width_override", screenSize.X);
		ProjectSettings.SetSetting("display/window/size/window_height_override", screenSize.Y);
		
		// 保存项目设置
		ProjectSettings.Save();
		
		GD.Print($"窗口大小已设置为: {screenSize.X} x {screenSize.Y}");
		GD.Print($"当前窗口实际大小: {window.Size.X} x {window.Size.Y}");
	}
}
